package design.simdi.code.final_paper.site.panels.licente.admisi;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
import org.apache.wicket.injection.web.InjectorHolder;
import org.apache.wicket.model.IModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.hibernate.criterion.Criterion;

import design.simdi.code.final_paper.core.daos.StudentAdmisDao;
import design.simdi.code.final_paper.core.entities.Person;
import design.simdi.code.final_paper.core.entities.Sesiune;
import design.simdi.code.final_paper.core.entities.StudentAdmis;

/**
 * 
 * @author Marius Dumitriu - (Jun 25, 2010 - 1:39:53 AM)
 * 
 */
public class SortableAdmisDataProvider extends SortableDataProvider<StudentAdmis> {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@SpringBean
	private StudentAdmisDao studentAdmisDao;

	private List<Criterion> criterii;
	private Sesiune sesiune;
	private Person owner;

	/**
	 * constructor
	 */
	public SortableAdmisDataProvider() {
		InjectorHolder.getInjector().inject(this);
		// set default sort
		setSort("id", true);
		criterii = new ArrayList<Criterion>();

	}

	/**
	 * @see org.apache.wicket.markup.repeater.data.IDataProvider#iterator(int,
	 *      int)
	 */
	public Iterator<StudentAdmis> iterator(int first, int count) {
		// SortParam sp = getSort();
		// Order o;
		// if (sp.isAscending())
		// o = Order.asc(sp.getProperty());
		// else
		// o = Order.desc(sp.getProperty());

		// return studentAdmisDao.findAll(new Order[] { o },
		// criterii).subList(first, first + count).iterator();
		String query;
		if (getOwner() != null && getOwner().getProfesor() != null)
			query = String
					.format(
							"select distinct sc from StudentAdmis as sc, Project as d , Person as p, Sesiune as s where sc.project = d  and d.author = p and p.id = '%s' and d.sesiune = s and s.id= '%s'",
							getOwner().getId(), getSesiune().getId());
		else
			query = String
					.format(
							"select distinct sc from StudentAdmis as sc, Project as d , Sesiune as s where sc.project = d  and d.sesiune = s and s.id= '%s'",
							getSesiune().getId());
		List<StudentAdmis> lista = studentAdmisDao.find(query);

		return lista.subList(first, first + count).iterator();

	}

	/**
	 * @see org.apache.wicket.markup.repeater.data.IDataProvider#size()
	 */
	public int size() {
		// return studentAdmisDao.findAll(new Order[] {}, criterii).size();
		String query;
		if (getOwner() != null && getOwner().getProfesor() != null)
			query = String
					.format(
							"select distinct sc from StudentAdmis as sc, Project as d , Person as p, Sesiune as s where sc.project = d  and d.author = p and p.id = '%s' and d.sesiune = s and s.id= '%s'",
							getOwner().getId(), getSesiune().getId());
		else
			query = String
					.format(
							"select distinct sc from StudentAdmis as sc, Project as d , Sesiune as s where sc.project = d  and d.sesiune = s and s.id= '%s'",
							getSesiune().getId());
		return studentAdmisDao.find(query).size();
	}

	/**
	 * @see org.apache.wicket.markup.repeater.data.IDataProvider#model(java.lang.Object)
	 */
	public IModel<StudentAdmis> model(StudentAdmis object) {
		return new DetachableAdmisModel(object, studentAdmisDao);
	}

	public void setCriterii(List<Criterion> criterii) {
		this.criterii = criterii;
	}

	public List<Criterion> getCriterii() {
		return criterii;
	}

	public void setStudentAdmisDao(StudentAdmisDao studentAdmisDao) {
		this.studentAdmisDao = studentAdmisDao;
	}

	public StudentAdmisDao getStudentAdmisDao() {
		return studentAdmisDao;
	}

	public void setSesiune(Sesiune sesiune) {
		this.sesiune = sesiune;
	}

	public Sesiune getSesiune() {
		return sesiune;
	}

	public void setOwner(Person owner) {
		this.owner = owner;
	}

	public Person getOwner() {
		return owner;
	}

}